//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.delivery;

import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.persistence.SQLList;
import org.jpgolay.me.sale.SalesPiece;
import org.jpgolay.me.util.MEDate;

public class DeliveryCRUD {

    public DeliveryCRUD() {
        init();
    }

    private void init() {
        sqlList = new SQLList();
        crud = new ObjectCRUD();
        date = new MEDate();
    }

    public Object[][] getDataList(String sql) {
        if (sqlList.search(sql)) {
            Object data[][] = sqlList.getRowsData();
            for (int i = 0; i < data.length; i++) {
                data[i][3] = date.toME((String) data[i][3]);
            }

            return data;
        } else {
            return null;
        }
    }

    public boolean setUpdateRow(String id, String deliveryMan, String deliveryDate, String deliveryNote) {
        SalesPiece piece = readPiece(id);
        if (piece != null) {
            piece.setDeliveryMan(deliveryMan);
            piece.setDeliveryDate(deliveryDate);
            piece.setNotes(deliveryNote);
            return updatePiece(piece);
        } else {
            return false;
        }
    }

    private SalesPiece readPiece(String id) {
        return (SalesPiece) crud.read(id, 22);
    }

    private boolean updatePiece(SalesPiece data) {
        return crud.update(data, 22);
    }
    private SQLList sqlList;
    private ObjectCRUD crud;
    private MEDate date;
}
